var app = sm("do_App");
var page = sm("do_Page");
var hashdata = mm("do_HashData");
var openHelper = require("helper/open");
var httpSilence = require("helper/httpSilence");
var qqHttp = require("helper/http");
var userUtil = require("helper/user");
var imHelper = require("helper/im");
var urlUser = require("url/user");
var urlCoupon = require("url/coupon");
var nf = deviceone.sm("do_Notification");
var sendDeliveryUtil = require("url/sendDelivery");
var urlMall = require("url/mall");
var urlOrder = require("url/order");
var imgMessageTip = ui("imgMessageTip");
/*圈子、社区、好友*/
ui("myCircleAout").on("touch", function(data, e) {
	openHelper.open("source://view/circle/base/manage/myCircle.ui");//我的圈子
});
ui("myActivityAout").on("touch", function(data, e) {
	openHelper.open("source://view/activity/list/myActivity.ui"); /***TODO:我参加的活动***/
});
ui("myFriendAout").on("touch", function(data, e) {
	openHelper.open("source://view/user/friend/myFriend.ui");
});

var aoutMain = ui("aoutMain");
/*我的动态*/
var addDynamic = aoutMain.add("dynamic", "source://view/user/cell/index.ui",0,0);
var dynamic = ui("dynamic");
var dataDynamic = [{"itemTitle":"邻里动态","menuIcon":"source://image/menu/u_00.png","alink":"source://view/community/manage/myMessage.ui"},
             {"itemTitle":"圈子动态","menuIcon":"source://image/menu/u_01.png","alink":"source://view/circle/topic/manage/myTopicList2.ui"},
             {"itemTitle":"我的评论","menuIcon":"source://image/menu/u_04.png","alink":"source://view/public/comment/manage/myComment2.ui"},
             {"itemTitle":"我的收藏","menuIcon":"source://image/menu/u_03.png","alink":"source://view/public/concern/manage/myFavorite.ui"}];
dynamic.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "我的动态",
	"isMore" : false,
    "mainItems" : dataDynamic
});
dynamic.refreshData();
ui(addDynamic+".mainItems").on("touch","", 300, function(data){
	openHelper.open(dataDynamic[data].alink);
})
/*我的订单*/
var addMyOrder = aoutMain.add("myOrder", "source://view/user/cell/index.ui","dynamic");
var myOrder = ui("myOrder");
var dataOrder = [{"itemTitle":"待付款","menuIcon":"source://image/menu/u_10.png"},
             {"itemTitle":"待发货","menuIcon":"source://image/menu/u_11.png"},
             {"itemTitle":"待收货","menuIcon":"source://image/menu/u_12.png"},
             {"itemTitle":"待退款","menuIcon":"source://image/menu/u_13.png"}];
myOrder.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "我的订单",
	"isMore" : true,
    "mainItems" : dataOrder
});
myOrder.refreshData();
ui(addMyOrder+".titleAout").on("touch", "", 300, function(data){//全部订单
	openHelper.open("source://view/order/list.ui",{},"","pay_product_back");
})
ui(addMyOrder+".mainItems").on("touch", "", 300, function(data){
	openHelper.open("source://view/order/listByStatus.ui",data,"","pay_product_back");
})

/*我的理财*/
var addFinancial = aoutMain.add("financial", "source://view/user/cell/index.ui","myOrder");
var financial = ui("financial");
var dataFinancial = [{"itemTitle":"优惠券","menuIcon":"source://image/menu/u_20.png","alink":"source://view/coupon/manage/list.ui"},
             {"itemTitle":"银行卡","menuIcon":"source://image/menu/u_22.png","alink":"source://view/user/bank/list.ui"},
             {"itemTitle":"余额","menuIcon":"source://image/menu/u_21.png","alink":"source://view/user/money/list.ui"},
             {"itemTitle":"圈币","menuIcon":"source://image/menu/u_23.png","alink":"source://view/honor/currency.ui"}];
financial.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "我的理财",
	"isMore" : false,
    "mainItems" : dataFinancial
});
financial.refreshData();
ui(addFinancial+".mainItems").on("touch","",300, function(data){
	openHelper.open(dataFinancial[data].alink);
})

/*我的业务*/
var addBusiness = aoutMain.add("business", "source://view/user/cell/index.ui","financial");
var business = ui("business");
var dataBusiness = [
             {"itemTitle":"我的店铺","menuIcon":"source://image/menu/u_30.png","alink":"source://view/qshop/manage/shop/myShop.ui"},
             {"itemTitle":"订单配送","menuIcon":"source://image/menu/u_32.png","alink":"source://view/sendDelivery/orderList.ui"},
             {"itemTitle":"我的技能","menuIcon":"source://image/menu/u_33.png","alink":"source://view/homemaking/manage/myList.ui"},
             {"itemTitle":"技能预约","menuIcon":"source://image/menu/u_34.png","alink":"source://view/homemaking/order/sellerList.ui"}];
business.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "我的业务",
	"isMore" : false,
    "mainItems" : dataBusiness
});
business.refreshData();

ui(addBusiness+".mainItems").on("touch","",300, function(data){
	if(data==0){//我的店铺
		httpSilence.post(urlMall.url.shop.hasCreate,{},function(data){
			if(data.hasCreate == 0){
				openHelper.open("source://view/qshop/manage/shop/addShop1.ui");
			} else {
				openHelper.open(dataBusiness[0].alink);
			}
		});
	}
	else if(data==1){//订单配送
		httpSilence.post(sendDeliveryUtil.url.user.isSendDeliveryUser, {}, function(data) {
			if (data.isSendDeliveryUser == 1) {
				openHelper.open(dataBusiness[1].alink,{"canAssigned" : data.canAssigned});
			} else {
				nf.toast("您还不是配送人员");
			}
		});
	}
	else if(data==7){//在线客服
		imHelper.chat2OnlineService();
	}
	else{//我的技能
		openHelper.open(dataBusiness[data].alink);
	}
})

/*客服中心*/
var addCustomerService = aoutMain.add("customerService", "source://view/user/cell/index.ui","business");
var customerService = ui("customerService");
var dataCustomerService = [
             {"itemTitle":"常见问题","menuIcon":"source://image/menu/u_36.png","alink":"source://view/user/help/list.ui"},
             {"itemTitle":"建议反馈","menuIcon":"source://image/menu/u_37.png","alink":"source://view/user/suggest/manage/list.ui"},
             {"itemTitle":"在线客服","menuIcon":"source://image/menu/u_38.png","alink":""}];
customerService.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "客服中心",
	"isMore" : false,
    "mainItems" : dataCustomerService
});
customerService.refreshData();
ui(addCustomerService+".mainItems").on("touch","",300, function(data){
	 if(data==2){//在线客服
			imHelper.chat2OnlineService();
	}else{//我的技能
		openHelper.open(dataCustomerService[data].alink);
	}
})

/****登录成功***/
/*头像*/
var aLayoutLogo=ui("aLayoutLogo");
var imgUserLogo = ui("imgUserLogo");
var aLayoutToDetail=ui("aLayoutToDetail");
var labelNick = ui("labelNick");
var labelPhone = ui("labelPhone");
var labelHonorValue = ui("do_Label_honorValue");
var labelHonorLevel = ui("do_Label_honorLevel");
var loginToken = userUtil.loginToken();
if(loginToken && loginToken.trim()!=""){
	addLoginFunc();
}
var userLogo;
var supplierId;
function addLoginFunc(){
	httpSilence.post(urlUser.url.base.get, {}, function(data) {
		userLogo=data.userBaseVo.bigFullLogo;
		imgUserLogo.source = data.loginUser.logo;
		labelNick.text = data.loginUser.nick;
		labelPhone.text = data.loginUser.phone;
		labelHonorValue.text = data.userValueVo.honorValue;
		labelHonorLevel.text = "v"+data.userValueVo.honorLevel;
	});
}
aLayoutToDetail.on("touch","",300, function(data,e){
	if(!loginToken || loginToken.trim()===""){
		openHelper.openLogin();
	}else{
		openHelper.open("source://view/user/base/userDetail.ui");
	}
});
aLayoutLogo.on("touch", function(data, e) {
	if(null!=userLogo&&''!=userLogo){
		var jsonImgs=[];
		jsonImgs.push({source:userLogo});
		openHelper.open("source://view/reuse/imgBrowser.ui", {
			imgDatas :JSON.stringify(jsonImgs),index:0
		});
	}else if(loginToken && loginToken.trim()!=""){
		openHelper.open("source://view/user/base/userDetail.ui");
	}else{
		openHelper.openLogin();
	}
});

app.on("fireUserUpdate",function(d){
	if(d.logo&&d.logo!==""){
		imgUserLogo.source=d.logo;
	}
    if(d.nick&&d.nick!==""){
    	labelNick.text=d.nick;
	}
});
page.on("result", function(data) {
	loginToken = userUtil.loginToken();
	if(loginToken && loginToken.trim()!=""){
		addLoginFunc();
	}
});
/*设置*/
var settingAout = ui("settingAout");
settingAout.on("touch", "", 3000,function(data, e) {
	if(!loginToken || loginToken.trim()===""){
		openHelper.openLogin();
	}else{
		openHelper.open("source://view/user/setting.ui");
	}
});

/*消息*/
var messageAout = ui("messageAout");
messageAout.on("touch", "", 3000,function(data, e) {
	if(!loginToken || loginToken.trim()===""){
		openHelper.openLogin();
	}else{
		openHelper.open("source://view/im/message/myMessage.ui");
		app.fire("qq_fire_hide_msg_tip");//隐藏消息小红点
	}
});

//显示消息小红点
app.on("qq_fire_show_msg_tip", function(data, e) {
	imgMessageTip.visible = true;
})
//隐藏消息小红点
app.on("qq_fire_hide_msg_tip", function(data, e) {
	imgMessageTip.visible = false;
})

app.on("fire_reflash_circle_count",function(data, e){
	reflashMyBizzInfo();
})
//响应点击主页底部【我的】刷新本页面动态数据
page.on("index_myself_tab_touch", function(data) {
	reflashMyBizzInfo();
	
});

//刷新【圈子、社区、好友】数量
function reflashMyBizzInfo(){
	clearDataBusinessCell();
	qqHttp.post(urlUser.url.base.myBizzInfo,{},function(data){
		ui("myCircleCount").text = data.myCircleCount;
		ui("myActivityCount").text = data.myActivityCount;  /***TODO:我参加的活动数***/
		ui("myFriendCount").text = data.myFriendCount;
	});
	shopOrderNumber();//我的店铺数量
	myOrderNumber();//我的订单数量【4类】
	myCouponNumber();
}
function clearDataBusinessCell(){
	dataBusiness[0].visible=false;
	dataBusiness[0].labelNum = 0;
	dataBusiness[1].visible=false;
	dataBusiness[1].labelNum = 0;
	dataBusiness[2].visible=false;
	dataBusiness[2].labelNum = 0;
	dataBusiness[3].visible=false;
	dataBusiness[3].labelNum = 0;
}
function shopOrderNumber(){
	httpSilence.post(urlOrder.url.base.shopOrderUncomplated,{},function(data){
		var uncompletedCount = data.uncompletedCount;
		if(uncompletedCount>0){
			dataBusiness[0].labelNum = uncompletedCount;
			dataBusiness[0].visible = true;
		}
		else{
			dataBusiness[0].labelNum = 0;
			dataBusiness[0].visible = false;
		}
		ui(addBusiness+".mainItems").items =  dataBusiness;
	});
}

//我的订单数量显示
function myOrderNumber(){
	httpSilence.post(urlOrder.url.base.myOrderCount,{},function(data){
		var waitPayCount = data.waitPayCount;//待付款
		var waitSendCount = data.waitSendCount;//待发货
		var waitReceiptCount = data.waitReceiptCount;//待收货
		var waitReturnCount = data.waitReturnCount;//待退款
		
		if(waitPayCount>0){//待付款
			dataOrder[0].labelNum = waitPayCount;
			dataOrder[0].visible = true;
		}else{
			dataOrder[0].labelNum = 0;
			dataOrder[0].visible = false;
		}
		if(waitSendCount>0){//待发货
			dataOrder[1].labelNum = waitSendCount;
			dataOrder[1].visible = true;
		}else{
			dataOrder[1].labelNum = 0;
			dataOrder[1].visible = false;
		}
		if(waitReceiptCount>0){//待收货
			dataOrder[2].labelNum = waitReceiptCount;
			dataOrder[2].visible = true;
		}else{
			dataOrder[2].labelNum = 0;
			dataOrder[2].visible = false;
		}
		if(waitReturnCount>0){//待退款
			dataOrder[3].labelNum = waitReturnCount;
			dataOrder[3].visible = true;
		}else{
			dataOrder[3].labelNum = 0;
			dataOrder[3].visible = false;
		}
		ui(addMyOrder+".mainItems").items =  dataOrder;
	});
}
function myCouponNumber()
{
httpSilence.post(urlCoupon.url.user.getUnusedCoupon,{},function(data){
	var couponCount = data.myCouponCount;
	if(couponCount>0){//优惠券数
		dataFinancial[0].labelNum = couponCount;
		dataFinancial[0].visible = true;
	}else{
		dataFinancial[0].labelNum = 0;
		dataFinancial[0].visible = false;
	}
	ui(addFinancial+".mainItems").items =  dataFinancial;
});
}

doSomeThing2MsgTip();
//处理消息红点是否提示
function doSomeThing2MsgTip(){
	var unreadCount = imHelper.getMyUnReadCount();
	//deviceone.print(unreadCount,"unreadCount-未读消息数量");
	if(parseInt(unreadCount) > 0){
		app.fire("qq_fire_show_msg_tip");//显示消息红点
	}else{
		app.fire("qq_fire_hide_msg_tip");//隐藏消息红点
	}
}

//头部透明度控制
var layoutHead = ui("layoutHead");
var scrollbody = ui("scrollbody");
var do_ImageView_4 = ui("do_ImageView_4");
var do_ImageView_5 = ui("do_ImageView_5");
var do_ImageView_7 = ui("do_ImageView_7");
var mainTitle = ui("mainTitle");
scrollbody.on("scroll",function(data){
	var top = data.top;
	var getnum = Math.ceil(top/10);
	var getendo = (getnum/2).toFixed(0);
	var getend = getnum-25;
	if(getend<-20){
		layoutHead.bgColor = "fafafa00";
		do_ImageView_4.source = "source://image/header/set.png";
		do_ImageView_5.source = "source://image/header/mess.png";
		do_ImageView_7.source = "source://image/header/newhelp.png";
		imgMessageTip.bgColor = "ffffffff";
		mainTitle.fontColor="00000000";
	}else if(getend<-10){
		getMainTitle("A");
		do_ImageView_4.source = "source://image/header/set2.png";
		do_ImageView_5.source = "source://image/header/message.png";
		do_ImageView_7.source = "source://image/header/newhelp2.png";
	}else if(getend<-15){
		getMainTitle("B");
	}else if(getend<-10){
		getMainTitle("C");
	}else if(getend<-5){
		getMainTitle("D");
	}else if(getend<0){
		getMainTitle("E");
	}else{
		getMainTitle("F");
	}
});
function getMainTitle(i){
	layoutHead.bgColor = "fafafa"+i+i;
	imgMessageTip.bgColor = "313131"+i+i;
	mainTitle.fontColor="313131"+i+i;
}

ui("headNewHelp").on("touch","", 300,function(data, e) {
	openHelper.open("source://view/honor/index/index.ui",{openShowIndex:1});
});
//我的荣誉等级
ui("do_LinearLayout_honorValue").on("touch", "", 300, function(data, e) {
	openHelper.open("source://view/honor/grade/index.ui");
});